using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using EventHandler = Utilities.EventHandler;
using Random = UnityEngine.Random;

public class PlatformManager : MonoBehaviour
{
    [SerializeField] private List<GameObject> platfromPrefabs;//生成的预制体
    private static Queue<PlatformController> currentPlatform = new Queue<PlatformController>();
    [SerializeField] private Vector3 SPAWNPOINT;
    private Vector3 oldPos;
    private Vector3 currentPos;
    private void Awake()
    {
        currentPlatform = new Queue<PlatformController>();
        currentPos = SPAWNPOINT;
        CreatePlatform();
        CreatePlatform();
    }
    private void OnEnable()
    {
        EventHandler.EnterTriggerColl += ChangePlatform;
    }

    private void OnDisable()
    {
        EventHandler.EnterTriggerColl -= ChangePlatform;
    }

    private void ChangePlatform()
    {
        StartCoroutine(CreatePlatformIE());
        StartCoroutine(DestroyPlatform());
    }

    IEnumerator CreatePlatformIE()
    {
        int value = Random.Range(0, platfromPrefabs.Count);
        GameObject obj = Instantiate(platfromPrefabs[value],currentPos,Quaternion.identity);
        yield return null;
        obj.transform.SetParent(transform);
        currentPlatform.Enqueue(obj.GetComponent<PlatformController>());
        oldPos = currentPos;
        currentPos += new Vector3(-30.8f, 14.362f, 0);
    }
    private void CreatePlatform()
    {
        int value = Random.Range(0, platfromPrefabs.Count);
        GameObject obj = Instantiate(platfromPrefabs[value],currentPos,Quaternion.identity);
        obj.transform.SetParent(transform);
        currentPlatform.Enqueue(obj.GetComponent<PlatformController>());
        oldPos = currentPos;
        currentPos += new Vector3(-30.8f, 14.362f, 0);
    }
    IEnumerator DestroyPlatform()
    {
        Destroy(currentPlatform.Peek().gameObject);
        yield return null;
        currentPlatform.Dequeue();
    }
    
    
}
